# Replicating DVG (2017): Within-Chain Rigidity in US Retail  
# JHL  

# Description: This folder contains do files to replicate DVG (2017), as well as steps required

# Steps 1-4 with R and parallelize with sbatch
# Steps 5-6 with Stata and parallelize with sbatch 
# Coded in dependencies to run as master sbatch file if necessary (Add #!/bin/bash to top of file and rename)

# Set path 
export PATH_HOME="/scratch/midway2/jleung/replication"
export RMS_MOVE="/project2/databases/nielsen/nielsen_extracts/RMS/Movement_Files_Combined_RFormatted"
export RMS_ANNUAL="/project2/databases/nielsen/nielsen_extracts/RMS/%d/Annual_Files"

# 1. Run through 06-14 product module level RData and create product x channel code ranks by revenue 
	# Run: 01_product_rank.R
	# with 01_product_rank.sbatch

jid1=$(sbatch --parsable 01_product_rank.sbatch)
		
# 2. Append all module-level product ranks, select 12 modules as in DVG, and top UPC by revenue (by channel code) 	
	# Run: 02_product_rank.R
	# with 02_product_rank.sbatch
	
jid2=$(sbatch --dependency=afterok:$jid1 --parsable 02_product_rank.sbatch) 
	
# 3. Extract RMS data of relevant UPC's  
	# Run: 03_product_rank.R
	# with 03_product_rank.sbatch	

jid3=$(sbatch --dependency=afterok:$jid2 --parsable 03_product_rank.sbatch) 
		
# 4. Calculate similarity measures
	# Run: 04_similarity_top1.R 
	# with 04_similarity_top1.sbatch 

jid4=$(sbatch --dependency=afterok:$jid3 --parsable 04_similarity_top1.sbatch) 
	
# 5. Collapse similarity measures at different levels of geography
	# Run: 05_similarity_top1.do 
	# with 05_similarity_top1_do.sbatch

jid5=$(sbatch --dependency=afterok:$jid4 --parsable 05_similarity_top1_do.sbatch) 
	
# 6. Further collapse similarity measures, merge with store characteristics 
	# Run: 06_similarity_top1.do 
	# with 06_similarity_top1.sbatch 
	
jid6=$(sbatch --dependency=afterok:$jid5 --parsable 06_similarity_top1.sbatch) 